package com.cwt_taxi_xa;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.cwt_taxi_xa.db.bll.CarCallSystemBiz;
import com.cwt_taxi_xa.protocol.DeviceBiz;

import java.text.SimpleDateFormat;
import java.util.Date;

public class BrightnessSetting extends CWTBaseActivity implements OnTouchListener{

	private static final String TAG = "BrightnessSetting";
	private RadioGroup rg_mode;
	private RadioButton mode_day;
	private RadioButton mode_night;
	private SeekBar sb;
	private boolean firstChange;
	private RadioGroup rg_sp;
	private RadioButton sp_close,sp_open;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.brightness_setting);

		firstChange = true;
		setupViews();
		addListener();
		initDate();
	}

	private void setupViews() {
		// TODO Auto-generated method stub
		rg_mode = (RadioGroup) findViewById(R.id.rg_mode);
		mode_day = (RadioButton) findViewById(R.id.mode_day);
		mode_night = (RadioButton) findViewById(R.id.mode_night);
		rg_sp = (RadioGroup) findViewById(R.id.rg_sp);
		sp_close = (RadioButton) findViewById(R.id.sp_close);
		sp_open = (RadioButton) findViewById(R.id.sp_open);
		sb = (SeekBar) findViewById(R.id.sb_brightness);
		sb.setMax(255);
	}

	private void addListener() {
		// TODO Auto-generated method stub
		rg_mode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup radiogroup, int i) {
				if(firstChange){
					firstChange = false;
					return;
				}
				switch (i) {
					case R.id.mode_day:
						DeviceBiz.setDayNightMode(0);
						String h = new SimpleDateFormat("HH").format(new Date());
						if(Integer.parseInt(h)>6&&Integer.parseInt(h)<18){
							sb.setProgress(255);
						}else{
							sb.setProgress(55);
						}
						sb.setEnabled(false);
						break;
					case R.id.mode_night:
						DeviceBiz.setDayNightMode(1);
						sb.setProgress(130);
						sb.setEnabled(true);
						break;
					default:
						break;
				}
			}
		});

		/*rg_sp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup radiogroup, int i) {
				BrightnessSetting.super.setCurrentTime(Constants.TIME_DETAIL);
				switch (i) {
				case R.id.sp_close:
					DeviceBiz.setScreenProtect(0);
					break;
				case R.id.sp_open:
					DeviceBiz.setScreenProtect(1);
					break;
				default:
					break;
				}
			}
		});*/

		sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
										  boolean fromUser) {
				// TODO Auto-generated method stub
				CarCallSystemBiz.setScreenBrightness(BrightnessSetting.this, progress);
				CarCallSystemBiz.saveScreenBrightness(BrightnessSetting.this, progress);
			}
		});
	}

	/**
	 * 初始化数据
	 */
	public void initDate(){

		switch (DeviceBiz.getScreenProtect()) {
			case 0:
				sp_open.setChecked(false);
				sp_close.setChecked(true);
				break;
			case 1:
				sp_open.setChecked(true);
				sp_close.setChecked(false);
				break;

			default:
				break;
		}
		switch (DeviceBiz.getDayNightMode()) {
			case 0:
				mode_night.setChecked(false);
				mode_day.setChecked(true);
				String h = new SimpleDateFormat("HH").format(new Date());
				if(Integer.parseInt(h)>6&&Integer.parseInt(h)<18){
					sb.setProgress(255);
				}else{
					sb.setProgress(55);
				}
				sb.setEnabled(false);
				break;
			case 1:
				mode_night.setChecked(true);
				mode_day.setChecked(false);
				int progress = CarCallSystemBiz.getScreenBrightness(this);
				sb.setProgress(progress);
				sb.setEnabled(true);
				break;

			default:
				break;
		}


	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return false;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}
}
